<template>
  <el-dialog
    :title="!dataForm.rId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="回复帖子编号" prop="tId">
      <el-input v-model="dataForm.tId" placeholder="回复帖子编号"></el-input>
    </el-form-item>
    <el-form-item label="用户编号" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户编号"></el-input>
    </el-form-item>
    <el-form-item label="回复表情" prop="rEmotion">
      <el-input v-model="dataForm.rEmotion" placeholder="回复表情"></el-input>
    </el-form-item>
    <el-form-item label="回复标题" prop="rTopic">
      <el-input v-model="dataForm.rTopic" placeholder="回复标题"></el-input>
    </el-form-item>
    <el-form-item label="回复内容" prop="rContents">
      <el-input v-model="dataForm.rContents" placeholder="回复内容"></el-input>
    </el-form-item>
    <el-form-item label="点击次数" prop="clickCount">
      <el-input v-model="dataForm.clickCount" placeholder="点击次数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          rId: 0,
          tId: '',
          userId: '',
          rEmotion: '',
          rTopic: '',
          rContents: '',
          clickCount: ''
        },
        dataRule: {
          tId: [
            { required: true, message: '回复帖子编号不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户编号不能为空', trigger: 'blur' }
          ],
          rEmotion: [
            { required: true, message: '回复表情不能为空', trigger: 'blur' }
          ],
          rTopic: [
            { required: true, message: '回复标题不能为空', trigger: 'blur' }
          ],
          rContents: [
            { required: true, message: '回复内容不能为空', trigger: 'blur' }
          ],
          clickCount: [
            { required: true, message: '点击次数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.rId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.rId) {
            this.$http({
              url: this.$http.adornUrl(`/bbl/reply/info/${this.dataForm.rId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.tId = data.reply.tId
                this.dataForm.userId = data.reply.userId
                this.dataForm.rEmotion = data.reply.rEmotion
                this.dataForm.rTopic = data.reply.rTopic
                this.dataForm.rContents = data.reply.rContents
                this.dataForm.clickCount = data.reply.clickCount
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bbl/reply/${!this.dataForm.rId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'rId': this.dataForm.rId || undefined,
                'tId': this.dataForm.tId,
                'userId': this.dataForm.userId,
                'rEmotion': this.dataForm.rEmotion,
                'rTopic': this.dataForm.rTopic,
                'rContents': this.dataForm.rContents,
                'clickCount': this.dataForm.clickCount
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
